package com.example.learningspringboot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;

@RestController
@RequestMapping("/redis")
@Slf4j
public class RedisController {

    private final RedisTemplate<String, Object> objectStringRedisTemplate;
    private final StringRedisTemplate stringRedisTemplate;

    @Autowired
    public RedisController(
            @Qualifier("objectStringRedisTemplate") RedisTemplate<String, Object> objectStringRedisTemplate,
            @Qualifier("stringRedisTemplate") StringRedisTemplate stringRedisTemplate
            ) {
        this.objectStringRedisTemplate = objectStringRedisTemplate;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @GetMapping("/setStringKeyAndValue")
    public String setStringKeyAndValue(){
        stringRedisTemplate.opsForValue().set("setStringKeyAndValue", "setStringKeyAndValue");
        String result = Objects.requireNonNull(stringRedisTemplate.opsForValue().get("setStringKeyAndValue"));
        log.info("setStringKeyAndValue result: {}",result);
        // 清理测试数据
        stringRedisTemplate.delete("setStringKeyAndValue");
        return result;
    }

    @PostMapping("/setObjectStringKeyAndValue")
    public String setObjectStringKeyAndValue(){
        objectStringRedisTemplate.opsForValue().set("setObjectStringKeyAndValue", 1);
        String result = Objects.requireNonNull(objectStringRedisTemplate.opsForValue().get("setObjectStringKeyAndValue")).toString();
        log.info("setObjectStringKeyAndValue result: {}",result);
        // 清理测试数据
        objectStringRedisTemplate.delete("setObjectStringKeyAndValue");
        return result;
    }
}
